package com.jaya.budan.activity;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.RelativeLayout;

import com.android.volley.Request;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.JsonObjectRequest;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.jaya.budan.R;
import com.jaya.budan.adapter.LV_My_Wallet_Jilu_Adapter;
import com.jaya.budan.base.BaseActivity;
import com.jaya.budan.bean.User_Wallet_JiLu;
import com.jaya.budan.connect.RequestConnect;
import com.jaya.budan.utils.ToolUtils;
import com.jaya.budan.utils.VolleyUtil;
import com.jaya.budan.views.MyListView;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.List;

public class My_Wallet_JiLu_Activity extends BaseActivity implements View.OnClickListener {

    private RelativeLayout back;
    private Button btn_wallet_jyjl_cz, btn_wallet_jyjl_sr, btn_wallet_jyjl_tx, btn_wallet_jyjl_xf;
    private MyListView lv_xyf_jilu;
    private String access_token;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_my_wallet_jyjilu);
        Intent intent = getIntent();
        access_token = intent.getStringExtra("access_token");
        back = findViewById(R.id.back);
        btn_wallet_jyjl_cz = findViewById(R.id.btn_wallet_jyjl_cz);
        btn_wallet_jyjl_sr = findViewById(R.id.btn_wallet_jyjl_sr);
        btn_wallet_jyjl_tx = findViewById(R.id.btn_wallet_jyjl_tx);
        btn_wallet_jyjl_xf = findViewById(R.id.btn_wallet_jyjl_xf);
        lv_xyf_jilu = findViewById(R.id.lv_xyf_jilu);
        back.setOnClickListener(this);
        btn_wallet_jyjl_cz.setOnClickListener(this);
        btn_wallet_jyjl_sr.setOnClickListener(this);
        btn_wallet_jyjl_tx.setOnClickListener(this);
        btn_wallet_jyjl_xf.setOnClickListener(this);
        postData(RequestConnect.gr_my_wallet_payment_jilu_Url + access_token + "&limit=10");//默认请求消费记录
    }

    @SuppressLint("NonConstantResourceId")
    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.btn_wallet_jyjl_cz:
                btn_wallet_jyjl_cz.setBackgroundResource(R.drawable.login_login);
                btn_wallet_jyjl_sr.setBackgroundResource(R.drawable.login_exit);
                btn_wallet_jyjl_tx.setBackgroundResource(R.drawable.login_exit);
                btn_wallet_jyjl_xf.setBackgroundResource(R.drawable.login_exit);
                postData(RequestConnect.gr_my_wallet_buy_jilu_Url + access_token + "&type=1");
                break;
            case R.id.btn_wallet_jyjl_sr:
                btn_wallet_jyjl_cz.setBackgroundResource(R.drawable.login_exit);
                btn_wallet_jyjl_sr.setBackgroundResource(R.drawable.login_login);
                btn_wallet_jyjl_tx.setBackgroundResource(R.drawable.login_exit);
                btn_wallet_jyjl_xf.setBackgroundResource(R.drawable.login_exit);
                postData(RequestConnect.gr_my_wallet_cash_jilu_Url + access_token + "&limit=10");
                break;
            case R.id.btn_wallet_jyjl_tx:
                btn_wallet_jyjl_cz.setBackgroundResource(R.drawable.login_exit);
                btn_wallet_jyjl_sr.setBackgroundResource(R.drawable.login_exit);
                btn_wallet_jyjl_tx.setBackgroundResource(R.drawable.login_login);
                btn_wallet_jyjl_xf.setBackgroundResource(R.drawable.login_exit);
                postData(RequestConnect.gr_my_wallet_income_jilu_Url + access_token + "&limit=10");
                break;
            case R.id.btn_wallet_jyjl_xf:
                btn_wallet_jyjl_cz.setBackgroundResource(R.drawable.login_exit);
                btn_wallet_jyjl_sr.setBackgroundResource(R.drawable.login_exit);
                btn_wallet_jyjl_tx.setBackgroundResource(R.drawable.login_exit);
                btn_wallet_jyjl_xf.setBackgroundResource(R.drawable.login_login);
                postData(RequestConnect.gr_my_wallet_payment_jilu_Url + access_token + "&limit=10");
                break;
            case R.id.back:
                finish();
                break;
            default:
                break;
        }
    }

    /**
     * 网络请求
     */
    private void postData(String url) {
        JsonObjectRequest request = new JsonObjectRequest(Request.Method.GET, url, null, new Response.Listener<JSONObject>() {
            @Override
            public void onResponse(JSONObject jsonObject) {
                Log.d("wallet", jsonObject.toString());
                try {
                    int code = jsonObject.optInt("code");
                    int status = jsonObject.optInt("status");
                    String message = jsonObject.getString("message");
                    if (code == 0) {
                        Gson gson = new Gson();
                        JSONArray info = jsonObject.getJSONArray("info");
                        List<User_Wallet_JiLu> mlist = gson.fromJson(info.toString(), new TypeToken<List<User_Wallet_JiLu>>(){}.getType());
                        LV_My_Wallet_Jilu_Adapter adapter = new LV_My_Wallet_Jilu_Adapter(My_Wallet_JiLu_Activity.this, mlist);
                        if (mlist.size() > 0){
                            lv_xyf_jilu.setAdapter(adapter);
                        } else {
                            adapter.notifyDataSetChanged();
                            lv_xyf_jilu.setAdapter(null);
                        }
                    } else {
                        ToolUtils.showToast(My_Wallet_JiLu_Activity.this, message);
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError volleyError) {
                ToolUtils.showToast(My_Wallet_JiLu_Activity.this, "网络请求失败，请稍后重试~");
            }
        });
        request.setTag(this);
        VolleyUtil.getQueue(this).add(request);
    }
}
